package top.ed333.mcplugins.configurableevents.action;

import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import top.ed333.mcplugins.configurableevents.DependenciesResolver;

import java.util.Collection;

public class ConsoleCmdAction extends Action {
    public ConsoleCmdAction(Object value) {
        super(value);
    }

    @Override
    public String name() {
        return "consoleCmd";
    }

    @Override
    public void execute(@Nullable Player player) {
        System.out.println("execute console cmd, player: " + player);
        if (value() instanceof Collection) {
            ((Collection<?>) value()).forEach(line -> {
                String cmdLine = DependenciesResolver.isPapiInstalled() ? PlaceholderAPI.setPlaceholders(player,  line.toString()) : line.toString();
                ConsoleCommandSender sender = Bukkit.getConsoleSender();
                Bukkit.dispatchCommand(sender, cmdLine);
            });
        } else {
            String cmdLine = DependenciesResolver.isPapiInstalled() ? PlaceholderAPI.setPlaceholders(player,  value().toString()) : value().toString();
            ConsoleCommandSender sender = Bukkit.getConsoleSender();
            Bukkit.dispatchCommand(sender, cmdLine);
        }
    }
}
